<?php
namespace app\admin\controller;
use x\Xbase;
use ETaobao\Factory;
class Task extends Xbase {

    //淘宝客更新数据
    public function tbk(){
      $day = date("Ymd");
      $rs = $this->db->query("select * from tbk_cat where day<?", [$day],1);
      if($rs){
        $user = $this->db->find('user',$rs['user_id']);
        $this->db->query("delete from tbk_item where tbk_cat_id=?",[$rs['id']] );
        //获取数据
        $config = [
            'appkey' => $user['tbk_app_key'],
            'secretKey' => $user['tbk_app_secret'],
            'format' => 'json',
            'sandbox' => false,
        ];
        $app = Factory::Tbk($config);
        $param = [
           'fields' => "num_iid,title,pict_url,small_images,zk_final_price_wap,status,coupon_click_url,coupon_info,coupon_remain_count,tk_rate,coupon_start_time,coupon_end_time,user_type,type,volume,reserve_price,category",
           'platform'=>2,
           'adzone_id'=>$rs['adzone_id'],
           'favorites_id'=>$rs['favorites_id'],
           'page_size'=>200,
           'page_no'=>1,
        ];
        $res = $app->uatm->getItemFavorites($param);
        $res = json_encode($res);
        $res = json_decode($res,true);
        $temp = [];
        if(isset($res['results']['uatm_tbk_item'])){
          foreach($res['results']['uatm_tbk_item'] as $key => $val){
            $res['results']['uatm_tbk_item'][$key]['favorites_id'] = $rs['favorites_id'];
          }
          $temp = $res['results']['uatm_tbk_item'];
        }
        $data = [];
        foreach($temp as $k => $v){
          //去掉没有券的商品,没有相册图,失效的
          if(!isset($temp[$k]['coupon_click_url']) || !isset($v['small_images']) || !isset($v['small_images']['string']) || $v['status']==0){
            continue;
          }
          $d = [
            'tbk_cat_id' => $rs['id'],
            'favorites_id' => $rs['favorites_id'],
            'user_id' => $user['id'],
            'num_iid' => $v['num_iid'],
            'title' => $v['title'],
            'pict_url' => $v['pict_url'],
            'small_images' => implode(',',$v['small_images']['string']),
            'zk_final_price_wap' => $v['zk_final_price_wap'],
            'coupon_click_url' => $v['coupon_click_url'],
            'coupon_info' => $v['coupon_info'],
            'coupon_remain_count' => $v['coupon_remain_count'],
            'tk_rate' => $v['tk_rate'],
            'coupon_start_time' => $v['coupon_start_time'],
            'coupon_end_time' => $v['coupon_end_time'],
            'user_type' => $v['user_type'],
            'type' => $v['type'],
            'status' => $v['status'],
            'volume' => $v['volume'],
            'reserve_price' => $v['reserve_price'],
            'favorites_id' => $v['favorites_id'],
            'category' => $v['category']
          ];
          $data[] = $d;
        }
        if($data){
          $this->db->insertAll('tbk_item',$data);
        }
        $this->db->update('tbk_cat', ['id'=>$rs['id'], 'day'=>$day]);
      }
      $this->exit(10);
    }

    //分润
    public function fenrun(){
        $rs = $this->db->query("select id from order_xiangmu where pay_status=1 and is_count=0 order by id asc",[],1);
        if(!$rs) $this->exit(10);
        s('orderxiangmu','c')->award($rs['id']);
        $this->exit(1);
    }

    //统计今日之前, 未统计的用户收益, 顺便统计分红
    public function usercountday(){
        $today = date("Ymd");
        $user = $this->db->query("select id,s_id from user where count_day<? order by id asc",[$today],1);
        if(!$user)$this->exit(10);
        s('usercountday','c')->doit($user);
        $this->exit(1);
    }

    //输出
    public function exit($s){
        echo $s;exit;
        e($s);
        js_url('',$s);
    }

}
